<?php
/**
 * Created by PhpStorm.
 * User: denh
 * Date: 16/12/15
 * Time: 下午6:45
 */
require_once ADMIN_PATH . '../bs/wxNews/Course.php';

class NewsApiController extends BaseController {

    Const SECRET_KEY = '95e6de86ef6e4f36b65440739ef5e654'; #接口秘钥

    function __construct($m = DEFAULT_CONTROLLER, $a = DEFAULT_METHOD) {
        parent::__construct($m , $a);
    }

    function meeting() {
        $tw_title = $this->request('tw_title', '', true);
        $tw_desc = $this->request('tw_desc', '');
        $tw_pic = $this->request('tw_pic', '', true);
        $template_first = $this->request('template_first', '');
        $template_keyword1 = $this->request('template_keyword1', '', true);
        $template_keyword2 = $this->request('template_keyword2', '', true);
        $template_keyword3 = $this->request('template_keyword3', '', true);
        $template_remark = $this->request('template_remark', '');
        $url = $this->request('url', '', true);
        if (!$this->checkSign()) {
            $this->jsonOutPut(-1, '签名错误');
        }

        $do = new DoNews_pictext_template();
        $do->set_pt_title($tw_title);
        $do->set_pt_desc($tw_desc);
        $do->set_pt_pic($tw_pic);
        $do->set_template_first($template_first);
        $do->set_template_keyword1($template_keyword1);
        $do->set_template_keyword2($template_keyword2);
        $do->set_template_keyword3($template_keyword3);
        $do->set_template_remark($template_remark);
        $do->set_url($url);
        $do->set_type('会议消息');
        if ($do->save()) {
            $this->run_cli('meet_msg', $do->get_id());
            $this->jsonOutPut(0, '触发成功');
        } else {
            $this->jsonOutPut(-1, '发送失败');
        }
    }

    /**
     * 课程预告
     */
    function course_notice_1() {
        $courseId              = $this->request('courseId', true);
        $pt_title              = $this->request('pt_title', true);
        $template_first        = $this->request('template_first', true);
        $template_courseName   = $this->request('template_courseName', true);
        $template_teacherName  = $this->request('template_teacherName', true);
        $template_remark       = $this->request('template_remark', true);

        $url = Tools::url('newCourse', 'detail', array('id' => $courseId));

        $do = new DoNews_pictext_template();
        $do->set_pt_title($pt_title);
        $do->set_template_first($template_first);
        $do->set_template_keyword1($template_courseName);
        $do->set_template_keyword2($template_teacherName);
        $do->set_template_remark($template_remark);
        $do->set_url($url);
        $do->set_note($courseId);
        if (!$do->save()) {
            $this->jsonOutPut(-1, 'err');
        }

        $this->run_cli('course_msg', $do->get_id());
        $this->jsonOutPut(0, '触发成功');
    }

    /**
     * 上课提醒
     */
    function course_notice_2() {
        $courseId             = $this->request('courseId', true);
        $template_courseName  = $this->request('template_courseName', true);
        $template_date        = $this->request('template_date', true);
        $template_remark      = $this->request('template_remark', true);

        $url = Tools::url('newCourse', 'detail', array('id' => $courseId));

        $do = new DoNews_pictext_template();
        $do->set_pt_title($template_courseName);
        $do->set_template_keyword1($template_courseName);
        $do->set_template_keyword2($template_date);
        $do->set_template_remark($template_remark);
        $do->set_url($url);
        $do->set_note($courseId);
        if (!$do->save()) {
            $this->jsonOutPut(-1, 'err');
        }

        $bs = new \bs\wxNews\Course_2('course_notice_2', $do->get_id());
        $bs->sendToSignUpUser();
        $this->jsonOutPut(0, 'ok');
    }

    /**
     * 判断接口签名
     * @return bool
     */
    private function checkSign() {
        $sign = $this->request('sign', '', true);

        $request = $_REQUEST;
        unset($request['sign']);
        ksort($request);
        foreach($request as $k => $v) {
            $request[$k] = $k . '=' . $v;
        }
        $sign_check = md5( implode('#', $request) . '#' . self::SECRET_KEY );

        if ($sign != $sign_check) {
            return false;
        }
        return true;
    }

}